package com.eastshop.user.adminaction;

import org.apache.struts2.ServletActionContext;

import com.eastshop.user.service.UserService;
import com.eastshop.user.vo.User;
import com.eastshop.utils.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
 * 后台用户管理的Action类
 * @author admin
 *
 */
public class UserAdminAction extends ActionSupport implements ModelDriven<User>{
	// 模型驱动使用的类
	private User user = new User();
	
	public User getModel() {
		return user;
	}

	// 注入用户的Service
	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	// 接收page
	private int page;

	public void setPage(int page) {
		this.page = page;
	}

	// 分页查询所有的用户
	public String findAllWithPage(){
		// 分页查询所有的用户
		PageBean<User> pageBean = userService.findAllWithPage(page);
		// 结果存入值栈
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		return "findAllWithPage";
	}
	
	// 跳转到编辑用户的界面
	public String edit(){
		// 根据id查询用户
		User user = userService.findByUid(this.user.getUid());
		ActionContext.getContext().getValueStack().set("user", user);
		return "edit";
	}
	
	// 编辑用户提交方法
	public String editSubmit(){
		userService.update(user);
		return "editSubmit";
	}
	
	// 删除用户
	public String delete(){
		// 根据id查询用户
		User currUser = userService.findByUid(user.getUid());
		// 删除用户
		userService.delete(currUser);
		return "delete";
	}
}
